// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/patrol/api/PatrolPointApi'

import ComClass from '@api/ComQuery'

import ListClass from '@api/patrol/model/PatrolPointListReq'
import OneClass from '@api/patrol/model/PatrolPointDetailReq'
import EditClass from '@api/patrol/model/PatrolPointUpdateReq'
import SubmitClass from '@api/patrol/model/PatrolPointSyncReq'
import BaseClass from '@api/patrol/model/PatrolPointDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'dto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'patrolPointList', DTO)
    return [resp.comQuery.totalCount, resp.list || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      item.oldAreaId = item.areaId
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'patrolPointDetail', DTO)
    return resp.dto || {}
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'patrolPointUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 设置 —————————————————————————————— //

  async submit(one, TIP = '设置') {
    const DTO = CORE.grpcDTO(SubmitClass, one)
    await CORE.grpc(TIP, CLIENT, 'patrolPointSync', DTO)
    return CORE.grpcOK(TIP)
  },
}
